// import 'package:device_info_plus/device_info_plus.dart';

import 'dart:convert';
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class DeviceUtil {
  static Future<String?> getDeviceId() async {
      final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

    // final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
    if(kIsWeb){}
    else if (Platform.isIOS) {
      final IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
      return iosInfo.identifierForVendor;
    } else if (Platform.isAndroid) {
      try {
        final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
        // return androidInfo.androidId;
        return androidInfo.id;
      } catch (e) {
        return null;
      }
    }else{
      var macs= await getMacAddresses();
      return macs.first;
    }
    return null;
  }




 // 根据操作系统执行对应命令获取MAC地址
  static Future<List<String>> getMacAddresses() async {
    List<String> macAddresses = [];
    try {
      if (Platform.isWindows) {
        // Windows系统：执行ipconfig /all命令
        final result = await Process.run('ipconfig', ['/all']);
        macAddresses = _parseWindowsOutput(result.stdout.toString());
      } else if (Platform.isMacOS || Platform.isLinux) {
        // macOS/Linux系统：执行ifconfig命令
        final result = await Process.run('ifconfig', []);
        macAddresses = _parseUnixOutput(result.stdout.toString());
      } else {
        print("不支持的操作系统");
      }
    } catch (e) {
      print("获取MAC地址失败：$e");
    }
    return macAddresses;
  }

  // 解析Windows系统的ipconfig输出
  static List<String> _parseWindowsOutput(String output) {
    List<String> macs = [];
    final lines = LineSplitter.split(output);
    String? adapterName;
    
    for (var line in lines) {
      line = line.trim();
      // 提取适配器名称
      if (line.startsWith('以太网适配器') || line.startsWith('无线局域网适配器')) {
        adapterName = line;
      }
      // 提取物理地址（MAC）
      else if (line.startsWith('物理地址') && adapterName != null) {
        final mac = line.split(':').last.trim();
        if (mac.isNotEmpty && mac != '媒体已断开连接') {
          macs.add("$adapterName: $mac");
        }
      }
    }
    return macs;
  }

  // 解析macOS/Linux系统的ifconfig输出
  static List<String> _parseUnixOutput(String output) {
    List<String> macs = [];
    final sections = output.split('\n\n');
    
    for (var section in sections) {
      if (section.isEmpty) continue;
      // 提取接口名称（如eth0、wlan0）
      final nameMatch = RegExp(r'^(\w+):').firstMatch(section);
      if (nameMatch == null) continue;
      final interfaceName = nameMatch.group(1);
      
      // 提取MAC地址（ether后面的内容）
      final macMatch = RegExp(r'ether\s+([0-9a-fA-F:]+)').firstMatch(section);
      if (macMatch != null && interfaceName != null) {
        final mac = macMatch.group(1);
        if (mac != null && mac.isNotEmpty) {
          macs.add("$interfaceName: $mac");
        }
      }
    }
    return macs;
  }

}
